package com.nut.service.impl;

import com.google.gson.Gson;
import com.nut.dao.BookDao;
import com.nut.dao.OrderDao;
import com.nut.dao.OrderItemDao;
import com.nut.dao.impl.BookDaoImpl;
import com.nut.dao.impl.OrderDaoImpl;
import com.nut.dao.impl.OrderItemDaoImpl;
import com.nut.pojo.*;
import com.nut.service.OrderService;
import com.nut.service.UserService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService{

    private final OrderDao orderDao = new OrderDaoImpl();
    private final OrderItemDao orderItemDao = new OrderItemDaoImpl();
    private final BookDao bookDao = new BookDaoImpl();
    private final UserService userService =new UserServiceImpl();

    @Override
    public String createOrderByList(Double totalPrice, List<CartItem> cart, Integer userId) {
        //订单号
        String orderId = System.currentTimeMillis()+ "" + userId;
        //创建一个订单对象
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        Date d= new Date();
        String str = sdf.format(d);
        Order order = new Order(orderId,str,totalPrice,0,userId);
        //保存订单
        orderDao.SaveOrder(order);
        for (CartItem cartItem :cart){
            //创建一个订单项对象
            OrderItem orderItem = new OrderItem(null,cartItem.getName(),cartItem.getCount(),cartItem.getPrice(),cartItem.getTotalPrice(),orderId);
            //保存订单项
            orderItemDao.SaveOrderItem(orderItem);
            //更新库存和销量
            Book book = bookDao.queryBookById(cartItem.getId());
            book.setSales(book.getSales() + cartItem.getCount());
            book.setStock(book.getStock() - cartItem.getCount());
            bookDao.updateBook(book);
        }
        //清空购物车
        cart.clear();
        return orderId;
    }
    /**
     * 创建订单
     * @param string
     * @return
     */
    @Override
    public String createOrder(String string) {
        Gson gson = new Gson();
        OrderVo orderVo = gson.fromJson(string, OrderVo.class);
        String username = orderVo.getUsername();
        List<CartItem> list = orderVo.getTableData();
        Double totalPrice = orderVo.getTotalPrice();
        String orderId = String.valueOf(System.currentTimeMillis()) ;
        Integer userId = userService.getUserIdByUsername(username);
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d= new Date();
        String str = sdf.format(d);
        orderDao.SaveOrder(new Order(orderId,str,totalPrice,0,userId));
        for (CartItem cartItem: list){
            OrderItem orderItem = toOrderItem(cartItem, orderId);
            orderItemDao.SaveOrderItem(orderItem);
        }
        return orderId;
    }
    private OrderItem toOrderItem(CartItem cartItem, String orderId) {
        String name = cartItem.getName();
        Double totalMoney = cartItem.getPrice()*cartItem.getCount();
        Double price = cartItem.getPrice();
        int count = cartItem.getCount();
        return new OrderItem(null,name,count,price,totalMoney, orderId);
    }
    @Override
    public List<Order> showMyOrders(int userId) {
        return orderDao.queryOrderByUserId(userId);
    }

    @Override
    public List<Order> showAllOrders() {
        return orderDao.queryOrders();
    }

    @Override
    public List<OrderItem> showOrderDetail(String orderId) {
        return orderItemDao.queryOrderItem(orderId);
    }

    @Override
    public void sendOrder(String orderId) {
        orderDao.updateStatus(1,orderId);
    }

    @Override
    public void receiveOrder(String orderId) {
        orderDao.updateStatus(2,orderId);
    }
}
